package com.xxxx.manager.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.manager.pojo.*;
import com.xxxx.manager.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.xxxx.manager.mapper.GoodsMapper;
import com.xxxx.manager.service.GoodsService;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
public class GoodsServiceImpl implements GoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;



    @Override
    public List<GoodsCategory> selectCategoryNameList() {
        return goodsMapper.selectCategoryNameList();
    }

    @Override
    public List<Brand> selectBrandNameList() {
        return goodsMapper.selectBrandNameList();
    }

    /**
     * 商品列表分页
     *
     * @param page
     * @return
     */
    @Override
    public Map<String, Object> selectPageFromGoodsAndCategory(Page page) {
        //开启分页
        PageHelper.startPage(page.getPageNum(), page.getPageSize());

        //准备返回对象
        HashMap<String, Object> map = new HashMap<>();

        //根据查询信息动态变化Redis的key
        String[] goodsPageListKeyArr = new String[]{
                "goods:pageNum_" + page.getPageNum() + ":pageSize_" + page.getPageSize() + ":",
                "catId_:",
                "brandId_:",
                "goodsName_:",
        };

        //根据查询传入的值动态改变RedisKey
        if (!StringUtils.isEmpty(page.getCatId())) goodsPageListKeyArr[1] = "catId_" + page.getCatId() + ":";
        if (!StringUtils.isEmpty(page.getBrandId())) goodsPageListKeyArr[2] = "brandId_" + page.getBrandId() + ":";
        if (!StringUtils.isEmpty(page.getGoodsName())) goodsPageListKeyArr[3] = "goodsName_" + page.getGoodsName() + ":";

        //将确定的后key转为String字符串
        String goodsKey = Arrays.stream(goodsPageListKeyArr).collect(Collectors.joining());
        System.out.println(goodsKey+"key==========请求了Redis....==================");

        //去Redis去获取数据
        ValueOperations<String,String> ops = redisTemplate.opsForValue();
        String pageInfoJson = ops.get(goodsKey);
        //如果Redis中有数据，就返回
        if (!StringUtils.isEmpty(pageInfoJson)){
            PageInfo<Goods> pageInfo = JSON.parseObject(pageInfoJson, PageInfo.class);
            map.put("code",200);
            map.put("pageInfo",pageInfo);
            return map;
        }

        //如果Redis中没有数据，就去请求数据库,并且返回
        List<Goods> goodsList = goodsMapper.selectPageFromGoodsAndCategory(page);

        if (goodsList != null && goodsList.size() > 0) {
            PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);
            map.put("code", 200);
            map.put("pageInfo", pageInfo);
            //将数据库的信息放入Redis
            ops.set(goodsKey,JSON.toJSONString(pageInfo));
            return map;
        }else {
            //如果没有数据。把空数据放入缓存。设置失效时间为60
            ops.set(goodsKey,JSON.toJSONString(new PageInfo<>(new ArrayList<Goods>())),60, TimeUnit.SECONDS);
        }

        //如果没有数据返回空和错误码
        map.put("code",500);
        map.put("pageInfo","");
        return map;
    }


}
